/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						       conversion_cache.h

	$Header: /game/conversion_cache.h $

	$NoKeywords: $

 ************************************************************************/

#ifndef CONVERSION_CACHE_H_INCLUDED
#define CONVERSION_CACHE_H_INCLUDED

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "resource_cache.h"

// -------------------------------------------------------------------
// cache which converts one type to another: implementation
// -------------------------------------------------------------------
template < typename source_type, typename result_type >
class t_conversion_cache_data : public t_abstract_resource_cache_data< result_type >
{
public:
	// Constructor/Destructor
	t_conversion_cache_data( std::string const & name );

private:
	// Member functions
	virtual char const * get_prefix() const;
	virtual result_type * do_read( std::streambuf & stream, t_progress_handler* handler );
};

template < typename source_type, typename result_type >
inline t_conversion_cache_data< source_type, result_type >::t_conversion_cache_data( std::string const & name )
	:	t_abstract_resource_cache_data< result_type >( name )
{
}

template < typename source_type, typename result_type >
char const * t_conversion_cache_data< source_type, result_type >::get_prefix() const
{
	return t_resource_traits< source_type >::prefix;
}

template < typename source_type, typename result_type >
result_type * t_conversion_cache_data< source_type, result_type >::do_read( std::streambuf & stream,
																		    t_progress_handler* handler )
{
	result_type * result = 0;
	source_type source;
	if ( ::read( stream, source ) )
		result = new result_type( source );
	return result;
}

// -------------------------------------------------------------------
// cache which converts one type to another
// -------------------------------------------------------------------
template <typename source_type, typename result_type>
class t_conversion_cache : public t_resource_cache<result_type>
{
public:
	t_conversion_cache( std::string const& name );
protected:
	typedef t_conversion_cache_data< source_type, result_type > t_conversion_data;

	t_conversion_cache( t_conversion_data* data );
};

template <typename source_type, typename result_type>
inline t_conversion_cache<source_type, result_type>::t_conversion_cache( std::string const& name )
{
	set( new t_conversion_data( name ) );
}
	
template <typename source_type, typename result_type>
inline t_conversion_cache<source_type, result_type>::t_conversion_cache( t_conversion_data* data )
{
	set( data );
}

#endif // CONVERSION_CACHE_H_INCLUDED
